/**
 * 评估相关 API
 */
import request, { ResponseData, PaginatedResponse } from '@/utils/request';

// 评估模式
export enum EvaluationMode {
  RULE_BASED = 'rule-based',
  AI_BASED = 'ai-based',
  HYBRID = 'hybrid',
}

// 问题类型
export enum IssueType {
  COMPLETENESS = 'completeness',
  STANDARDIZATION = 'standardization',
  CONSISTENCY = 'consistency',
  ACCURACY = 'accuracy',
}

// 问题严重程度
export enum IssueSeverity {
  CRITICAL = 'critical',
  MAJOR = 'major',
  MINOR = 'minor',
  INFO = 'info',
}

// 评估状态
export enum AssessmentStatus {
  PENDING = 'pending',
  PROCESSING = 'processing',
  COMPLETED = 'completed',
  FAILED = 'failed',
}

// 问题详情
export interface AssessmentIssue {
  id: string;
  issue_type: IssueType;
  severity: IssueSeverity;
  field_name?: string;
  issue_description: string;
  suggestion?: string;
  score_deduction: number;
}

// 维度评分
export interface DimensionScores {
  completeness: number;
  standardization: number;
  consistency: number;
  accuracy: number;
}

// 评估结果
export interface Assessment {
  id: string;
  record_id: string;
  evaluation_mode: EvaluationMode;
  overall_score: number;
  completeness_score: number;
  standardization_score: number;
  consistency_score: number;
  accuracy_score: number;
  summary?: string;
  suggestions?: string[];
  assessed_at: string;
  assessment_duration?: number;
  status: AssessmentStatus;
}

// 评估详情
export interface AssessmentDetail extends Assessment {
  issues: AssessmentIssue[];
}

// 评估请求
export interface AssessmentRequest {
  record_id: string;
  evaluation_mode?: EvaluationMode;
}

// 批量评估请求
export interface BatchAssessmentRequest {
  record_ids: string[];
  evaluation_mode?: EvaluationMode;
}

// 批量评估响应
export interface BatchAssessmentResponse {
  total: number;
  success: number;
  failed: number;
  results: Assessment[];
  errors: Array<{ record_id: string; error: string }>;
}

// 评估统计
export interface AssessmentStatistics {
  total_records: number;
  assessed_records: number;
  average_score: number;
  score_distribution: Record<string, number>;
  common_issues: Array<{ type: string; count: number }>;
}

/**
 * 评估单个病历
 */
export const evaluateRecord = (data: AssessmentRequest) => {
  return request.post<ResponseData<AssessmentDetail>>(
    '/assessment/evaluate',
    data
  );
};

/**
 * 批量评估病历
 */
export const batchEvaluate = (data: BatchAssessmentRequest) => {
  return request.post<ResponseData<BatchAssessmentResponse>>(
    '/assessment/batch-evaluate',
    data
  );
};

/**
 * 获取评估列表
 */
export const getAssessmentsList = (page: number = 1, page_size: number = 10) => {
  return request.get<ResponseData<PaginatedResponse<Assessment>>>(
    '/assessment/list',
    { params: { page, page_size } }
  );
};

/**
 * 获取评估详情
 */
export const getAssessmentDetail = (assessmentId: string) => {
  return request.get<ResponseData<AssessmentDetail>>(
    `/assessment/${assessmentId}`
  );
};

/**
 * 获取病历的最新评估
 */
export const getRecordLatestAssessment = (recordId: string) => {
  return request.get<ResponseData<AssessmentDetail>>(
    `/assessment/record/${recordId}`
  );
};

/**
 * 删除评估记录
 */
export const deleteAssessment = (assessmentId: string) => {
  return request.delete<ResponseData<{ assessment_id: string }>>(
    `/assessment/${assessmentId}`
  );
};

/**
 * 获取评估统计
 */
export const getAssessmentStatistics = () => {
  return request.get<ResponseData<AssessmentStatistics>>(
    '/assessment/statistics/summary'
  );
};

